(function ($) {
    "use strict";
    $.fn.pin = function (options) {
        var scrollY = 0, elements = [], disabled = false, $window = $(window), $this = $(this);

        options = options || {};
        options.top = options.top || 0;

        var oldStyle = $this.attr("style") || "";


        var onScroll = function () {
//            console.log(arguments)
            $this.attr({style: oldStyle});
            if(options.minWidth && window.innerWidth <= options.minWidth){
                return
            }else{
                if(($this.offset().top - document.body.scrollTop) < options.top){
                    $this.css({
                        width: $this.outerWidth(),
                        position: "fixed",
                        top: options.top + "px",
                        left: $this.offset().left + "px"
                    })
                }
            }
            (options.afterScroll || function(){})($this)
        };

        $window.scroll(onScroll);
        $window.resize(onScroll);
        $window.load(onScroll);

        return this;
      };
})(jQuery);
